Skip to content

Conversation

penelopeysm
Copy link
Member

@penelopeysm penelopeysm commented Aug 7, 2025

Previously we had resetlogp!!(vi). This set the varinfo's logp field to zero.

Now we don't just have logp but also other kinds of accumulators. In general, everywhere we called resetlogp!! (e.g. at the beginning of model evaluation), we do also want to zero out all the other accumulators.

This PR introduces DynamicPPL.reset(acc), and DynamicPPL.resetaccs!!(vi) which resets every acc.

Note that resetaccs!! is not exported. Should we? The only place in Turing that uses resetlogp!! is particle MCMC (in fact I'm unsure whether it really needs it, but that's another matter).

In general pMCMC also uses setacc!!, which is definitely unexported. We also have a bunch of fiddling around with accumulators (e.g. ValuesAsInModelAcc), the constructors for which are all unexported.

Copy link
Contributor

github-actions bot commented Aug 7, 2025

Benchmark Report for Commit 4a275a9

Computer Information

Julia Version 1.11.6
Commit 9615af0f269 (2025-07-09 12:58 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 4 × AMD EPYC 7763 64-Core Processor
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, znver3)
Threads: 1 default, 0 interactive, 1 GC (on 4 virtual cores)

Benchmark Results

|                 Model | Dimension |  AD Backend |      VarInfo Type | Linked | Eval Time / Ref Time | AD Time / Eval Time |
|-----------------------|-----------|-------------|-------------------|--------|----------------------|---------------------|
| Simple assume observe |         1 | forwarddiff |             typed |  false |                  8.7 |                 1.5 |
|           Smorgasbord |       201 | forwarddiff |             typed |  false |                625.5 |                42.9 |
|           Smorgasbord |       201 | forwarddiff | simple_namedtuple |   true |                406.7 |                53.8 |
|           Smorgasbord |       201 | forwarddiff |           untyped |   true |               1142.6 |                30.2 |
|           Smorgasbord |       201 | forwarddiff |       simple_dict |   true |               6385.6 |                29.7 |
|           Smorgasbord |       201 | reversediff |             typed |   true |               1007.1 |                40.5 |
|           Smorgasbord |       201 |    mooncake |             typed |   true |                965.7 |                 4.3 |
|    Loop univariate 1k |      1000 |    mooncake |             typed |   true |               5539.4 |                 3.9 |
|       Multivariate 1k |      1000 |    mooncake |             typed |   true |                954.6 |                 8.9 |
|   Loop univariate 10k |     10000 |    mooncake |             typed |   true |              62739.7 |                 3.5 |
|      Multivariate 10k |     10000 |    mooncake |             typed |   true |               8087.5 |                10.2 |
|               Dynamic |        10 |    mooncake |             typed |   true |                130.1 |                12.1 |
|              Submodel |         1 |    mooncake |             typed |   true |                 12.2 |                 5.3 |
|                   LDA |        12 | reversediff |             typed |   true |                974.3 |                 2.5 |

Comment on lines +35 to +40
function Base.:(==)(
acc1::PointwiseLogProbAccumulator{wlp1}, acc2::PointwiseLogProbAccumulator{wlp2}
) where {wlp1,wlp2}
return (wlp1 == wlp2 && acc1.logps == acc2.logps)
end

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately the test that I wrote (checking that all the accs are == reset(acc) originally broke because most of them don't have an appropriate == method defined, so I had to introduce these (which mimic other changes we've made previously). I wonder if there's a better way of doing this than all the boilerplate that I introduced here though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a better way of doing this than all the boilerplate that I introduced here though.

I suspect not, not without some sketchy metaprogramming or doing a loop over fieldnames or something else that feels dangerous.

Comment on lines +33 to 35
function _zero(acc::ValuesAsInModelAccumulator)
return ValuesAsInModelAccumulator(empty(acc.values), acc.include_colon_eq)
end
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I preferred using _zero over Base.zero because the latter would effectively make this method public, and it doesn't seem like there's any reason to, since the public interface is split and reset.

Copy link

codecov bot commented Aug 7, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 82.30%. Comparing base (ea6b6de) to head (4a275a9).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1013      +/-   ##
==========================================
+ Coverage   82.16%   82.30%   +0.14%     
==========================================
  Files          38       38              
  Lines        3935     3945      +10     
==========================================
+ Hits         3233     3247      +14     
+ Misses        702      698       -4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@coveralls
Copy link

coveralls commented Aug 7, 2025

Pull Request Test Coverage Report for Build 16827424307

Details

  • 40 of 40 (100.0%) changed or added relevant lines in 10 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.1%) to 82.558%

Totals Coverage Status
Change from base Build 16810321891: 0.1%
Covered Lines: 3247
Relevant Lines: 3933

💛 - Coveralls

Copy link
Contributor

github-actions bot commented Aug 7, 2025

DynamicPPL.jl documentation for PR #1013 is available at:
https://TuringLang.github.io/DynamicPPL.jl/previews/PR1013/

@penelopeysm penelopeysm changed the title resetaccs!! resetaccs!! before evaluating Aug 7, 2025
@penelopeysm penelopeysm requested a review from mhauru August 7, 2025 15:33
Copy link
Member

@mhauru mhauru left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very happy with this.

I don't have strong opinions on exports. I never know what to think of as the external facing interface of DPPL, when its only user is Turing.jl. In some sense we would be fine exporting nothing, or exporting everything.


# Force single-threaded execution.
DynamicPPL.evaluate_threadunsafe!!(model, varinfo)
_, varinfo = DynamicPPL.evaluate_threadunsafe!!(model, varinfo)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good spot.

Comment on lines +35 to +40
function Base.:(==)(
acc1::PointwiseLogProbAccumulator{wlp1}, acc2::PointwiseLogProbAccumulator{wlp2}
) where {wlp1,wlp2}
return (wlp1 == wlp2 && acc1.logps == acc2.logps)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there's a better way of doing this than all the boilerplate that I introduced here though.

I suspect not, not without some sketchy metaprogramming or doing a loop over fieldnames or something else that feels dangerous.

@penelopeysm
Copy link
Member Author

I'll merge but will hold off on the release

@penelopeysm penelopeysm added this pull request to the merge queue Aug 8, 2025
Merged via the queue into main with commit b1241b1 Aug 8, 2025
20 of 21 checks passed
@penelopeysm penelopeysm deleted the py/resetaccs branch August 8, 2025 16:34
penelopeysm added a commit to TuringLang/Turing.jl that referenced this pull request Aug 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants